package com.alexgaoyh.MutiModule.Dubbo.RWSeperate.consumer.web.mutiDatabase;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.MutiModule.common.twitter.IDGenerator.instance.IdWorkerInstance;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.api.mutiDatabase.read.IMutiDatabaseReadService;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.api.mutiDatabase.write.IMutiDatabaseWriteService;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.persist.mutiDatabase.MutiDatabase;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.persist.mutiDatabase.MutiDatabaseExample;
import com.alibaba.dubbo.config.annotation.Reference;

/**
*
* dubbo 消费者
*
* @Reference 注解需要在 dubbo consumer中做如下配置
*
* <dubbo:annotation/>
*	<context:component-scan base-package="net.aimeizi.dubbo.controller">
*	<context:include-filter type="annotation" expression="com.alibaba.dubbo.config.annotation.Reference"/>
* </context:component-scan>
*
* 若要使用@Autowired或@Resource注解需要显式声明bean
*
* 使用@Autowired或@Resource注解时需要使用dubbo:reference来声明
* <dubbo:reference interface="net.aimeizi.dubbo.service.UserService" id="userService"/>
* <dubbo:reference interface="net.aimeizi.dubbo.service.DemoService" id="demoService"/>
*
* 以上的配置均需要在spring mvc的DispatcherServlet配置中显式配置dubbo consumer的配置.如/WEB-INF/applicationContext-dubbo-consumer.xml 否则在Controller中服务报NullPointException
* <servlet>
*	<servlet-name>mvc-dispatcher</servlet-name>
*		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
*	<init-param>
*	<param-name>contextConfigLocation</param-name>
*		<param-value>/WEB-INF/applicationContext*.xml,/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
*	</init-param>
*	<load-on-startup>1</load-on-startup>
* </servlet>
*
*/
@Controller
@RequestMapping(value="mutiDatabase")
public class MutiDatabaseController {

	@Reference(group="readService", interfaceName="com.alexgaoyh.MutiModule.Dubbo.RWSeperate.api.mutiDatabase.read.IMutiDatabaseReadService")
	private IMutiDatabaseReadService mutiModuleReadService;
	
	@Reference(group="writeService", interfaceName="com.alexgaoyh.MutiModule.Dubbo.RWSeperate.api.mutiDatabase.write.IMutiDatabaseWriteService")
	private IMutiDatabaseWriteService mutiModuleWriteService;

	@RequestMapping(value = "/index")
	public ModelAndView index(ModelAndView model, HttpServletRequest request) {
		MutiDatabaseExample example = new MutiDatabaseExample();
		example.setOrderByClause("id desc");
		List<MutiDatabase> list = mutiModuleReadService.selectByExample(example);
		model.addObject("list", list);
		model.setViewName("mutiDatabase/index");
		return model;
	}
	
	
	@RequestMapping(value = "/save")
	public ModelAndView save(ModelAndView model, HttpServletRequest request) {
		MutiDatabase record = new MutiDatabase();
		record.setId(IdWorkerInstance.getId());
		record.setName(new Date().toString());
		
		mutiModuleWriteService.insert(record);
		
		model.setViewName("redirect:index");
		return model;
	}
	
	@RequestMapping(value = "/saveTransaction")
	public ModelAndView saveTransaction(ModelAndView model, HttpServletRequest request) {
		
		MutiDatabase record1 = new MutiDatabase();
		record1.setId(IdWorkerInstance.getId());
		record1.setName(new Date().toString());
		
		MutiDatabase record2 = new MutiDatabase();
		
		mutiModuleWriteService.insertTransaction(record1, record2);
		
		model.setViewName("redirect:index");
		return model;
	}
}
